package com.sgb.member.view_model;

import com.sgb.member.PickCateType;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.ui.fragment.MemberTypeFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class MemberTypeModel extends BaseViewModel {

    private final MemberTypeFragment fragment;
    private MemberRepertory repertory;

    private int current;

    public MemberTypeModel(MemberTypeFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    // 关注企业会员
    public void getMemberCustomers(boolean isLoadMore, String companyName, String mNo0, String mNo01, String mNo1, String mNo3) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName); //搜索企业名称
        map.put("memberType", mNo1); //会员类型
        map.put("companyType", mNo3); //企业类型
        map.put("type", mNo01); //1需方会员 2供方会员
        map.put("inviType", mNo01);
        map.put("limit", 20);
        map.put("page", currentPage);
        repertory.getApi().findMemberCustomerList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }
        });
    }

    // 企业关注的会员
    public void getMemberCompanys(boolean isLoadMore, String companyName, String mNo0, String mNo01, String mNo1, String mNo3) {
        int currentPage = isLoadMore ? current + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName); //搜索企业名称
        map.put("memberType", mNo1); //会员类型
        map.put("companyType", mNo3); //企业类型
        map.put("type", mNo01); //1需方会员 2供方会员
        map.put("inviType", mNo01);
        map.put("limit", 20);
        map.put("page", currentPage);
        repertory.getApi().findMemberCompanyList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }
        });
    }

    public void userFocus(String businessNo, boolean isGz, int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("businessNo", businessNo);
        map.put("type", 0); // 固定值0
        DialogHelper.showProgressDialog(fragment.getContext(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().userFocus(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                fragment.updateMemberFollow(isGz, position);
            }
        });
    }

    //  会员类型 0 供方 1需方
    public void getLeftChooseData(int type, boolean isShowLeftDialog, boolean isDemandMember) {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        Call<BaseEntity<List<CommonPopEntity>>> call = null;
        switch (type) {
            case PickCateType.TYPE_0:
                // 会员主体
                List<CommonPopEntity> listMain = new ArrayList<>();
                listMain.add(new CommonPopEntity("1", "企业会员"));
                listMain.add(new CommonPopEntity("0", "个人会员"));
                if (isShowLeftDialog) {
                    fragment.showLeftChooseDialog(type, listMain);
                } else {
                    fragment.setLeftChooseData(type, listMain);
                }
                break;
            case PickCateType.TYPE_1:
                // 会员类型 0 供方 1需方
                String ascription = isDemandMember ? "1" : "0";
                call = repertory.getApi().getMemberTypeList(ascription);
                break;
            case PickCateType.TYPE_3:
                // 企业类型
                call = repertory.getApi().getCompanyTypeList();
                break;
            case PickCateType.TYPE_01:
                // 1需方会员 、 2供方会员
                List<CommonPopEntity> list = new ArrayList<>();
                list.add(new CommonPopEntity("1", "需方会员"));
                list.add(new CommonPopEntity("2", "供方会员"));
                if (isShowLeftDialog) {
                    fragment.showLeftChooseDialog(type, list);
                } else {
                    fragment.setLeftChooseData(type, list);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }
        if (call == null) {
            DialogHelper.dismissProgressDialog();
            return;
        }
        call.enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null) {
                    data.getData().add(0, new CommonPopEntity("", "全部"));
                    if (isShowLeftDialog) {
                        fragment.showLeftChooseDialog(type, data.getData());
                    } else {
                        fragment.setLeftChooseData(type, data.getData());
                    }
                }
            }
        });
    }

   /* public void getCompanyTypes() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getCompanyTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null) {
                    data.getData().add(0, new CommonPopEntity("", "全部"));
                    fragment.showCompanyTypes(data.getData());
                }
            }
        });
    }*/
}
